#include "Object.h"

CObject::CObject()
{
	m_collisionResponse = m_pos = D3DXVECTOR2(0, 0);
	this->m_isAlive = this->m_isVisible = this->m_isCollision = TRUE;
	m_velocity = D3DXVECTOR2(0, 0);
	m_acceleration = D3DXVECTOR2(0, 0);
}

CObject::CObject(D3DXVECTOR2 pos)
{
	this->m_pos = pos;
	this->m_isAlive = this->m_isVisible = this->m_isCollision = TRUE;
	m_velocity = D3DXVECTOR2(0, 0);
	m_acceleration = D3DXVECTOR2(0, 0);
}

void CObject::Init()
{
}

OBJECT_TYPE CObject::GetType()
{
	return OBJECT;
}

void CObject::UpdateBond()
{
	m_rectBond.left = (long)m_pos.x;
	m_rectBond.right = (long)(m_rectBond.left + m_sprite->GetFrameWidth());
	m_rectBond.top = (long)m_pos.y;
	m_rectBond.bottom = (long)(m_rectBond.top + m_sprite->GetFrameHeight());
}


void CObject::UpdateResponse(float gamTime, CInput* input)
{
	m_pos += m_collisionResponse * gamTime;
}

void CObject::Update(float gameTime, CInput* input)
{
	UpdatePosition(gameTime, input);
	//m_sprite->Update(gameTime);
}

void CObject::UpdatePosition(float gameTime, CInput* input)
{
	m_posLast = m_pos;
	m_velocityLast = m_velocity;
	m_velocity += m_acceleration * gameTime;
	m_pos += m_velocity * gameTime + 1.0f/2 * m_acceleration * gameTime * gameTime;
	//m_pos += m_velocity;
	if (m_pos.x <= 0)
	{
		m_pos.x = 0;
	}
	UpdateBond();
}

void CObject::UpdatePosition(float collisionTime)
{
	m_posLast = m_pos;
	m_velocityLast = m_velocity;
	/*m_velocity += m_acceleration * gameTime;
	m_pos += m_velocity * gameTime + 1.0f/2 * m_acceleration * gameTime * gameTime;*/
	m_pos += m_velocity * collisionTime;
	//m_pos += m_velocity;
	if (m_pos.x <= 0)
	{
		m_pos.x = 0;
	}
	UpdateBond();
}

void CObject::UpdateAnimation(float gameTime, CInput* input)
{

}

void CObject::Draw(LPD3DXSPRITE spriteHandlder, CCamera* camera)
{
	D3DXMATRIX temp, matrix, temp2;

	spriteHandlder->GetTransform(&temp);
	D3DXMatrixTranslation(&matrix, m_pos.x, m_pos.y, 0);
	D3DXMatrixMultiply(&matrix, &temp, &matrix);
	temp2 = camera->GetCameraMatrix();
	D3DXMatrixMultiply(&matrix, &matrix, &temp2);
	spriteHandlder->SetTransform(&matrix);
	if (this->m_isAlive)
		this->m_sprite->Draw(spriteHandlder);
	spriteHandlder->SetTransform(&temp);
}

bool CObject::IsCollideWith(CObject* another)
{
	RECT rectOther = another->GetBond();
	RECT des;
	if (IntersectRect(&des, &rectOther, &m_rectBond))
		return true;
	else
		return false;
}

RECT CObject::GetIntersectBond(CObject* another)
{
	RECT rectOther = another->GetBond();
	RECT des;
	IntersectRect(&des, &rectOther, &m_rectBond);
	return des;
}

bool CObject::IsPointIn(D3DXVECTOR2 point)
{
	if ((point.x >= m_rectBond.left) && (point.x <= m_rectBond.right) && (point.y >= m_rectBond.top) && (point.y <= m_rectBond.bottom))
		return true;
	else
		return false;
}

void CObject::SetLastPosition()
{
	m_pos = m_posLast;
	m_velocity = m_velocityLast;
	UpdateBond();
}